﻿using PlaneClean.Models;
using PlaneCleanEntity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// plc运行反馈记录
    /// </summary>
    public class OperationRecordController: ManagerController
    {
        /// <summary>
        /// 查询飞机清洗记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<t_operation_record> List(PageListRequest<v_t_operation_record> request)
        {
            if (request.pageIndex <= 0) request.pageIndex = 1;
            if (request.pageSize <= 0) request.pageSize = int.MaxValue;
            int total = 0;
            ListResponse<t_operation_record> response = new ListResponse<t_operation_record>();
            response.bodyList = new List<t_operation_record>();
            response.result = "NG";
            var sugar = DbFactory.dbFactory.Db.Queryable<t_operation_record>()
                .Where(a => a.or_id > 0);
            if (!string.IsNullOrEmpty(request.body.or_operator))
            {
                sugar.Where(a => a.or_operator.Contains(request.body.or_operator));
            }
            if (request.body.record_time != null && request.body.record_time.Count > 0)
            {
                sugar.Where(a => Convert.ToDateTime(request.body.record_time[0])
                < a.or_receive_time && a.or_receive_time < Convert.ToDateTime(request.body.record_time[1]));
            }
            sugar.OrderBy(a => a.or_receive_time, OrderByType.Desc);
            //加上where 加快查询速度 (where 中使用索引条件)
            List<t_operation_record> orList = sugar.ToPageList(request.pageIndex, request.pageSize, ref total);

            response.bodyList.AddRange(orList);
            response.total = total;

            response.result = "OK";
            return response;
        }
    }
}